using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using Microsoft.Extensions.Logging;
using RBAC.ErrorCode;
using RBAC.Domain.Dto.Output;
using RBAC.Read.Api.Application.Command.ERP.PurchasingManagement.RawMaterialSuppliers;
using RBAC.Read.Api.Application.Command.ERP.PurchasingManagement.PurchaseOrders;
using RBAC.Read.Api.Application.Command.ERP.PurchasingManagement.PurchaseInvoicefolder;
using RBAC.Read.Api.Application.Command.ERP.PurchasingManagement.PurchaseArrivalNotes;
using RBAC.Read.Api.Application.Dto.ERP;
using System;
using System.Collections.Generic;
using RBAC.Domain.WarehouseManagement;
using RBAC.Read.Api.Application.Command.ERP.PurchasingManagement.PurchaseReturns;

namespace RBAC.Read.Api.Controllers.ERP
{
    /// <summary>
    /// 采购管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PurchasingManagementController : ControllerBase
    {
        private readonly ILogger<PurchasingManagementController> _logger;
        private readonly IMediator _mediator;

        public PurchasingManagementController(ILogger<PurchasingManagementController> logger, IMediator mediator)
        {
            _logger = logger;
            _mediator = mediator;
        }

        /// <summary>
        /// 获取供应商列表（分页 + 条件查询）
        /// </summary>
        /// <param name="request">查询参数：页码、每页、供应商名称(模糊)、业务联系人(模糊)、供应料品(精确)</param>
        [HttpGet]
        public Task<APIPaging<GetRawMaterialSupplierListDto>> GetRawMaterialSuppliers([FromQuery] GetRawMaterialSuppliersListCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 导出供应商列表为Excel（根据筛选条件）
        /// </summary>
        /// <param name="request">筛选参数：供应商名称(模糊)、业务联系人(模糊)、供应料品(精确)</param>
        [HttpGet]
        public async Task<IActionResult> ExportRawMaterialSuppliers([FromQuery] ExportRawMaterialSuppliersCommand request)
        {
            var bytes = await _mediator.Send(request);
            var fileName = $"Suppliers_{DateTime.Now:yyyyMMdd_HHmmss}.xls";
            const string contentType = "application/vnd.ms-excel";
            return File(bytes, contentType, fileName);
        }

        /// <summary>
        /// 采购订单与采购物料联查（分页）
        /// </summary>
        [HttpGet]
        public Task<APIPaging<Read.Api.Application.Dto.ERP.PurchaseOrderWithMaterialDto>> GetPurchaseOrdersWithMaterials([FromQuery] GetPurchaseOrdersWithMaterialsCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 采购订单主表列表（分页）
        /// </summary>
        [HttpGet]
        public Task<APIPaging<PurchaseOrderListDto>> GetPurchaseOrders([FromQuery] GetPurchaseOrdersListCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 采购到货单主表列表（分页）
        /// </summary>
        [HttpGet]
        public Task<APIPaging<ArrivalNoteListDto>> GetArrivalNotes([FromQuery] GetArrivalNotesListCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 采购到货单与到货物料联查（分页）
        /// </summary>
        [HttpGet]
        public Task<APIPaging<ArrivalNoteWithMaterialDto>> GetArrivalNotesWithMaterials([FromQuery] GetArrivalNotesWithMaterialsCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取采购发票列表（分页 + 条件查询）
        /// </summary>
        /// <param name="request">查询参数：页码、每页、单据号(模糊)、供应商、发票日期范围</param>
        [HttpGet]
        public Task<APIPaging<PurchaseInvoiceListDto>> GetPurchaseInvoices([FromQuery] GetPurchaseInvoiceCommands request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 导出采购发票（含物料）到Excel（根据筛选条件）
        /// </summary>
        /// <param name="request">筛选参数：单据号(模糊)、供应商、发票日期范围</param>
        [HttpGet]
        public async Task<IActionResult> ExportPurchaseInvoices([FromQuery] GetExportPurchaseInvoiceCommands request)
        {
            var bytes = await _mediator.Send(request);
            var fileName = $"PurchaseInvoices_{DateTime.Now:yyyyMMdd_HHmmss}.xls";
            const string contentType = "application/vnd.ms-excel";
            return File(bytes, contentType, fileName);
        }

        /// <summary>
        /// 物料列表（分页 + 条件查询）
        /// </summary>
        /// <param name="request">查询参数：页码、每页、物料编码(模糊)、物料名称(模糊)、物料分类或编码前缀</param>
        [HttpGet]
        public Task<APIPaging<MaterListDto>> GetMaterials([FromQuery] GetMaterCommands request)
        {
            return _mediator.Send(request);
        }

       

        /// <summary>
        /// 采购发票明细表（联查发票+供应商+物料）分页列表
        /// </summary>
        /// <param name="request">查询参数：单据号、供应商、发票日期范围、发票号、税号、物料编码/名称 等</param>
        [HttpGet]
        public Task<APIPaging<PurchaseInvoiceDetailRowDto>> GetPurchaseInvoiceDetails([FromQuery] GetPurchaseInvoiceDetailsCommands request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 采购退货单主表列表（分页 + 条件）
        /// </summary>
        /// <param name="request">查询参数：页码、每页、跟单编号(模糊)、单据编码(模糊)、物料编码(模糊)、物料名称(模糊)</param>
        [HttpGet]
        public Task<APIResult<APIPaging<PurchaseReturn>>> GetPurchaseReturns([FromQuery] GetPurchaseReturnsCommands request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 采购退货单 与 物料 联查（分页 + 条件）
        /// </summary>
        /// <param name="request">查询参数：页码、每页、单据编码、跟单编号、物料编码/名称（均模糊）</param>
        [HttpGet]
        public Task<APIPaging<PurchaseReturnWithMaterialDto>> GetPurchaseReturnsWithMaterials([FromQuery] GetPurchaseReturnsWithMaterialsCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 销售退货明细列表（分页）
        /// </summary>
        [HttpGet]
        public Task<APIResult<APIPaging<SalesReturnDetails>>> GetSalesReturnDetails([FromQuery] GetSalesReturnDetailsCommands request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 打印：销售退货明细列表（不分页，返回全部未删除数据）
        /// </summary>
        [HttpGet]
        public Task<APIResult<List<SalesReturnDetails>>> PrintSalesReturnDetails([FromQuery] PrintSalesReturnDetailCommands request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 导出销售退货明细为Excel
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> ExportSalesReturnDetails([FromQuery] ExportSalesReturnDetailCommands request)
        {
            var bytes = await _mediator.Send(request);
            var fileName = $"SalesReturnDetails_{DateTime.Now:yyyyMMdd_HHmmss}.xls";
            const string contentType = "application/vnd.ms-excel";
            return File(bytes, contentType, fileName);
        }
    }
}
